package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/3 7:55 AM
 */
public class LC_232 {
    static class MyQueue {

        Stack<Integer> stack;

        /**
         * Initialize your data structure here.
         */
        public MyQueue() {
            stack = new Stack<>();
        }

        /**
         * Push element x to the back of queue.
         */
        public void push(int x) {
            stack.push(x);
        }

        /**
         * Removes the element from in front of queue and returns that element.
         */
        public int pop() {
            Stack<Integer> stack1 = new Stack<>();
            sw(stack1, stack);
            if (!stack1.empty()) {
                Integer pop = stack1.pop();
                sw(stack, stack1);
                return pop;
            }
            throw new RuntimeException();
        }

        private void sw(Stack<Integer> ps, Stack<Integer> po) {
            while (!po.empty()) {
                ps.push(po.pop());
            }
        }

        /**
         * Get the front element.
         */
        public int peek() {
            Stack<Integer> stack1 = new Stack<>();
            sw(stack1, stack);
            if (!stack1.empty()) {
                Integer peek = stack1.peek();
                sw(stack, stack1);
                return peek;
            }
            throw new RuntimeException();
        }

        /**
         * Returns whether the queue is empty.
         */
        public boolean empty() {
            return stack.empty();
        }
    }

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
}
